//
//  MasterViewController.m
//  Công thức
//
//  Created by NS Chuong on 13/11/12.
//  Copyright (c) 2012 Tamsao. All rights reserved.
//

#import "MasterViewController.h"

#import "DetailTableViewController.h"

@interface MasterViewController () {
    NSMutableArray *_objects;
}
@end

@implementation MasterViewController
@synthesize tableView = _tableView;
@synthesize subjectList, iconImages,bannerIsVisible;

-(void)dealloc{
    
    [subjectList release];
    [iconImages release];
    [_tableView release];
    [super dealloc];
}



- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    //self.navigationItem.leftBarButtonItem = self.editButtonItem;

   // UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
  //  self.navigationItem.rightBarButtonItem = addButton;
    //self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
    
    self.subjectList = [[[NSArray alloc]initWithObjects: @"Hình học",
                   @"Đại số",
                   @"Lượng giác",
                   @"Phương trình",
                   @"Hình học Giải tích",
                   @"Đạo hàm Vi phân",
                   @"Tích phân",
                         @"Về tác giả",nil]autorelease];;
    
    self.iconImages = [[[NSArray alloc]
                       initWithObjects:@"icon_HH.png",
                       @"icon_DS.png",
                       @"icon_LG.png",
                       @"icon_PhuongTrinh.png",
                       @"icon_GiaiTich.png",
                       @"icon_DaoHam.png",
                       @"icon_TichPhan.png",
                       @"logo_s.png",nil]autorelease];
    
    self.title=@"Công thức";
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return subjectList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];    
    if (cell== nil){
        cell=[[UITableViewCell alloc] initWithStyle:
              UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        
    }
    // Configure the cell...
    cell.textLabel.text= [subjectList objectAtIndex:indexPath.row];
    
    cell.accessoryType= UITableViewCellAccessoryDisclosureIndicator;
    
    UIImage *iconPhoto = [UIImage imageNamed:
                          [self.iconImages objectAtIndex: [indexPath row]]];
    
    cell.imageView.image=iconPhoto;
    
    return cell;
}

/*
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}*/



- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
    if(!self.bannerIsVisible)
    {
        
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:1];
        [banner setAlpha:1];
        // Assumes the banner view is just off the bottom of the screen.
        //banner.frame = CGRectOffset(banner.frame, 0, self.view.frame.size.height-50);
        banner.frame = CGRectOffset(banner.frame, 0,14);
        
        [UIView commitAnimations];
        
        self.bannerIsVisible = YES;
    }
    
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
    if(!self.bannerIsVisible)
    {
        
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:1];
        [banner setAlpha:0];
        // Assumes the banner view is just off the bottom of the screen.
        //banner.frame = CGRectOffset(banner.frame, 0, self.view.frame.size.height-50);
        //  banner.frame = CGRectOffset(banner.frame, 0, 50.0f);
        [UIView commitAnimations];
        
        self.bannerIsVisible = NO;
    }
}



/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/



- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    DetailTableViewController * DVC = [[DetailTableViewController alloc]init];
    
    DVC = [segue destinationViewController];
    
    NSIndexPath * path= [self.tableView indexPathForSelectedRow];
    NSString * theSubject = [subjectList objectAtIndex:path.row];
    
    DVC.subjectNumber = path.row;
    DVC.subjectName = theSubject;
    
}

@end
